<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @GetCategoryId="GetCategoryId" :show="scene == 0" />
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!Category3id"
          @click="addSPU"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template v-slot="{ row, $index }">
              <!-- hindButton是自己二次封装的element组件 主要是练习v-bind="$attrs" 和v-on="$listeners" -->
              <hindButton
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSkuInfo(row)"
              ></hindButton>
              <hindButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="editSPU(row)"
              ></hindButton>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="详细"
                style="margin: 0 5px"
                @click="handle(row)"
              ></el-button>
              <el-popconfirm
                title="你确定要删除吗？"
                @onConfirm="Deletespu(row)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除"
                  slot="reference"
                  style="margin: 0 5px"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          align="center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout=" prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="sizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @getspuFormscene="getspuFormscene"
        ref="Spu"
      />
      <SkuForm v-show="scene == 2" ref="skuForm" @changescenes="changescenes" />
    </el-card>
    <el-dialog
      title="sku列表"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" border style="width: 100%" v-loading="loading">
        <el-table-column
          property="skuName"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="200"
        ></el-table-column>
        <el-table-column property="weight" label="重量"></el-table-column>
        <el-table-column property="" label="默认图片">
          <template v-slot="{ row, index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: { SpuForm, SkuForm },
  data() {
    return {
      Category1id: "",
      Category2id: "",
      Category3id: "",

      limit: 3,
      page: 1,
      records: [],
      total: 0,
      scene: 0,
      dialogTableVisible: false,
      skuList: [],
      loading: true,
    };
  },
  methods: {
    GetCategoryId({ id, index }) {
      if (index == 1) {
        this.Category1id = id;
        this.Category2id = "";
        this.Category3id = "";
      } else if (index == 2) {
        this.Category2id = id;
        this.Category3id = "";
      } else {
        this.Category3id = id;
        //当三级id存在时，发送请求获得数据，展示数据
        this.getSpuList();
      }
    },
    //获得spu列表的回调 和点击页码的回调
    async getSpuList(currentpage = 1) {
      this.page = currentpage;
      const { Category3id, limit, page } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, Category3id);
      if (result.code == 200) {
        // console.log(result);
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //每页显示的spu个数的回调
    sizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //添加SPU按钮的回调
    addSPU() {
      this.scene = 1;
      this.$refs.Spu.initAddSpuData(this.Category3id);
      //清空数据
      Object.assign(this.$refs.Spu._data, this.$refs.Spu.$options.data());
    },
    //修改spu的回调
    editSPU(row) {
      this.scene = 1;
      //向子组件传数据
      this.$refs.Spu.initSpuData(row);
    },
    //改变scene的值 (自定义事件的回调)
    getspuFormscene({ scene, flag }) {
      this.scene = scene;
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    //删除某个spu
    async Deletespu(row) {
      // console.log(row);
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      // console.log(result);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //添加sku信息
    addSkuInfo(row) {
      this.scene = 2;
      // console.log(row);
      this.$refs.skuForm.getDate(this.Category1id, this.Category2id, row);
    },
    changescenes(scene) {
      this.scene = scene;
    },
    //查看sku列表并保存数据
    async handle(row) {
      this.dialogTableVisible = true;
      let result = await this.$API.spu.reSkuList(row.id);
      console.log(result);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },
    //sku列表关闭前的回调
    close(done) {
      this.loading = true;
      //清除sku列表的数据
      this.skuList = [];
      done();
    },
  },
};
</script>

<style>
</style>